// 03myCenter/03myOptions/myOptions.js
var co = require('../../resource/js/common.js')
var M = require('../../resource/js/md5.js')
var app = getApp()
var _this

Page({

    /*页面的初始数据*/
    data: {
        userInfo: '',
        ifAlter: 0,
        // 密码
        oldPassWord: '',
        newPassWord: '',
        // 密码框
        isOldPassWord: true,
        isNewPassWord: true,
        // 显隐图片
        oldImage: 'noSee.png',
        newImage: 'noSee.png',
        // 提示信息
        oldInfo: '',
        newInfo: '',
        // 登录还是支付密码
        alterID: ''
    },

    /* 生命周期函数--监听页面加载*/
    onLoad: function (options) {
        _this = this
        // console.log("【获取用户信息】===", app.Data.userInfo)
        this.setData({
            userInfo: app.Data.userInfo
        })
    },
    // 关闭
    close: function () {
        this.setData({
            ifAlter: 0,
            oldInfo: '',
            newInfo: ''
        })
    },
    // 修改
    alter: function (e) {
        this.setData({
            ifAlter: 1,
            alterID: e.currentTarget.id,
            oldPassWord: '',
            newPassWord: ''
        })
    },
    // 旧密码
    oldPassWord: function (e) {
        // console.log("【旧密码】===", e.detail.value)
        this.setData({
            oldPassWord: e.detail.value,
            oldInfo: ''
        })
    },
    // 新密码
    newPassWord: function (e) {
        // console.log("【新密码】===", e.detail.value)
        this.setData({
            newPassWord: e.detail.value,
            newInfo: ''
        })
    },
    // 清空
    clear: function (e) {
        var oldPassWord,newPassWord,oldInfo,newInfo
        if (e.currentTarget.id == 0) {
            oldPassWord = ''
        } else {
            newPassWord = ''
        }
        this.setData({
            newPassWord: newPassWord,
            oldPassWord: oldPassWord
        })
    },
    // 密码可见
    see: function (e) {
        if (e.currentTarget.id == 0) {
            var o, oi
            if (this.data.isOldPassWord == true) {
                o = false
                oi = 'see.png'
            } else {
                o = true
                oi = 'noSee.png'
            }
            this.setData({
                isOldPassWord: o,
                oldImage: oi
            })
        } else {
            var n, ni
            if (this.data.isNewPassWord == true) {
                n = false
                ni = 'see.png'
            } else {
                n = true
                ni = 'noSee.png'
            }
            this.setData({
                isNewPassWord: n,
                newImage: ni
            })
        }
    },
    // 修改密码
    upDateInfo: function (e) {
        var oldInfo, newInfo, param, ifAlter
        var ol = this.data.oldPassWord
        var ne = this.data.newPassWord
        if (this.data.alterID == 0) {
            if (co.empty(ol)) {
                oldInfo = '请输入旧登录密码！'
            }
            if (co.empty(ne)) {
                newInfo = '请输入新登录密码！'
            }
            if (!co.empty(ol) && !co.empty(ne)) {
                param = {
                    User_ID: app.Data.userInfo.User_ID,
                    User_Password: M.md5(ol),
                    User_Password_New: M.md5(ne)
                }
            }
        } else {
            if (co.empty(ol)) {
                oldInfo = '请输入旧支付密码！'
            }
            if (co.empty(ne)) {
                newInfo = '请输入新支付密码！'
            }
            if (!co.empty(ol) && !co.empty(ne)) {
                param = {
                    User_ID: app.Data.userInfo.User_ID,
                    User_PayPassword: M.md5(ol),
                    User_PayPassword_New: M.md5(ne)
                }
            }
        }
        console.log("【获取param】===",param)
        co.getShareApi('user/Mg/updateInfo', param, function (res) {
            console.log("【获取返回信息】===", res)
            if (res.code == 29375){
                oldInfo = res.msg
                // newInfo = res.msg
            }else if(res.code == 0){
                ifAlter = 0
                co.toast(res.msg)
            }
            _this.setData({
                oldInfo: oldInfo,
                newInfo: newInfo,
                ifAlter: ifAlter
            })
        })
    }
})